package XML.T5;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class T5 {

    public static void main(String[] args) throws DocumentException {

        ArrayList<Book>list = new ArrayList<>();

        SAXReader sr = new SAXReader();
        Document doc = sr.read("book.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            Book b = new Book(
                    Integer.parseInt(element.attributeValue("id")),
                    element.elementText("title"),
                    element.elementText("publishDate"),
                    Integer.parseInt(element.elementText("price"))
            );
            list.add(b);
        }

        // 1.
        for (Book b : list) {
            if (b.getId()>=2){
                System.out.println(b);
            }
        }
        System.out.println("--------------");

        // 2.
        for (Book b : list) {
            if (b.getTitle().contains("Java")){
                System.out.println(b);
            }
        }
        System.out.println("--------------");

        // 3.
        for (Book b : list) {
            if (b.getPrice()>50){
                System.out.println(b);
            }
        }
    }
}